<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="编号" width="80">
        <template slot-scope="{row}">
          <router-link :to="'/usr/edit/'+row.DocEntry" class="link-type">
            <span>{{ row.DocEntry }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="用户编号">
        <template slot-scope="scope">
          <span>{{ scope.row.Code }}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="用户名称">
        <template slot-scope="scope">
          <span>{{ scope.row.Name }}</span>
        </template>
      </el-table-column>

      <el-table-column v-if="false" width="250px" align="center" label="用户组">
        <template slot-scope="scope">
          <span>{{ scope.row.U_BOUSG }}</span>
        </template>
      </el-table-column>

      <el-table-column width="250px" align="center" label="用户组名">
        <template slot-scope="scope">
          <span>{{ scope.row.BOUSG_Name }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="260">
        <template slot-scope="scope">
          <router-link :to="'/usr/edit/'+scope.row.DocEntry">
            <el-button type="primary" size="small" icon="el-icon-edit">
              更新基础信息
            </el-button>
          </router-link>

          <el-button v-if="isAdmin" style="margin-left:10px;" type="success" size="small" icon="el-icon-edit" @click="onOpenPwdUpdate(scope.row.Code)">
            修改密码
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogTableVisible" title="修改密码">
      <el-form ref="postForm" :model="postForm" label-width="80px" style="width:400px;">
        <el-form-item label="新密码">
          <el-input v-model="postForm.U_Pwd" placeholder="请输入新密码, 密码不能为空"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onUpdateUserPassword">提交</el-button>
          <el-button @click="dialogTableVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import axios from 'axios'
import { fetchBOUSRs, fetchBOUSGs, updateUserPasswordBOUSRs } from '@/api/res'
import { getUserInfo } from '@/utils/auth'
import Pagination from '@/components/Pagination'

export default {
  name: 'UsrList',
  components: { Pagination },
  data() {
    return {
      list: null,
      flist: null,
      total: 0,
      dialogTableVisible: false,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      postForm:{},
      BOUSGs:[],
      isAdmin:false,
    }
  },
  created() {

    fetchBOUSGs({ page:1, limit:200}).then(res=>{
       const { flist } = res
       console.log(flist)
       this.BOUSGs = flist
       this.getList()
    })
  },
  mounted() {
    var _userInfo = JSON.parse(getUserInfo())

    this.userInfoRoles = JSON.parse(_userInfo.U_Menu).role

    if(this.userInfoRoles.indexOf('C1') >= 0 && this.userInfoRoles.indexOf('C2') >= 0 && this.userInfoRoles.indexOf('C3') >= 0){
      this.isAdmin = true
    }else{
      this.isAdmin = false
    }

  },
  methods: {
    onUpdateUserPassword(){
      console.log('onUpdateUserPassword-------',this.postForm)
      let config = {
        Code: this.postForm.Code,
        U_Pwd: this.postForm.U_Pwd.trim()
      }
      if(config.U_Pwd.length == 0){
        this.$message({ message: '新密码不能为空', type: 'error'})
        return
      }

      updateUserPasswordBOUSRs(config).then(res=>{

        this.$message({ message: '密码更新成功', type: 'success'})
        this.dialogTableVisible = false;
        console.log('updateUserPasswordBOUSRs--ok-----',res)

      }).catch(err=>{
        console.log('updateUserPasswordBOUSRs--er-----',err)
      })
    },

    onOpenPwdUpdate(Code){
      this.dialogTableVisible=true;
      this.postForm = { 'Code': Code, 'U_Pwd':'' }
      console.log('onOpenPwdUpdate-------',this.dialogTableVisible,this.postForm)
    },

    getList() {
      this.listLoading = true

      fetchBOUSRs(this.listQuery).then(res=>{
        var { flist, curPageList} = res

        curPageList = curPageList.map(v=>{
          v.BOUSG_Name = this.BOUSGs.filter(m=>m.Code == v.U_BOUSG)[0].Name
          return v
        })

        console.log(curPageList)

        this.flist = flist
        this.list = curPageList
        this.total = flist.length

        this.listLoading = false

      }).catch(err=>{
        console.log('error submit!!',err);
        this.listLoading = false
      })

    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
